home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / tbfile.c < prev    next >
C/C++ Source or Header  |  1995-09-08  |  902b  |  53 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include "internal.h"
  6.  
  7. PUBLIC TBFILE::TBFILE(
  8.     const char *_defpath)    // Default path
  9. {
  10.     defpath = strdup(_defpath);
  11.     nbopen = 0;
  12.     cur = NULL;
  13. }
  14.  
  15. PUBLIC TBFILE::~TBFILE()
  16. {
  17.     while (nbopen > 0) ::fclose (tb[--nbopen]);
  18.     free (defpath);
  19. }
  20.  
  21.  
  22. /*
  23.     Open a new file and remember the one currently open.
  24.     (put in on a stack). fclose will get it back.
  25.  
  26.     Return the FILE handle or NULL if can't open.
  27. */
  28. PUBLIC FILE *TBFILE::fopen (const char *fname, const char *mode)
  29. {
  30.     char abspath[PATH_MAX];
  31.     if (fname[0] != '/'){
  32.         sprintf (abspath,"%s/%s",defpath,fname);
  33.         fname = abspath;
  34.     }
  35.     FILE *ret = xconf_fopen (fname,mode);
  36.     if (ret != NULL){
  37.         tb[nbopen++] = ret;
  38.         cur = ret;
  39.     }
  40.     return ret;
  41. }
  42.  
  43. PUBLIC void TBFILE::fclose ()
  44. {
  45.     if (nbopen > 0){
  46.         ::fclose (cur);
  47.         cur = NULL;
  48.         nbopen--;
  49.         if (nbopen > 0) cur = tb[nbopen-1];
  50.     }
  51. }
  52.  
  53.